What is ms-rest?
The ms-rest npm package is a runtime library for REST clients generated by AutoRest. It provides functionalities for making HTTP requests, handling responses, and managing authentication, among other things.
What are ms-rest's main functionalities?
HTTP Client
This feature allows you to send HTTP requests and handle responses. The code sample demonstrates how to create a ServiceClient instance and send a GET request to a specified URL.
const msRest = require('ms-rest');
const client = new msRest.ServiceClient();
client.sendRequest({
method: 'GET',
url: 'https://api.example.com/data'
}).then(response => {
console.log(response.body);
}).catch(err => {
console.error(err);
});
Authentication
This feature provides support for various authentication mechanisms. The code sample demonstrates how to authenticate using a service principal and then send an authenticated GET request.
const msRest = require('ms-rest');
const msRestAzure = require('ms-rest-azure');
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, (err, credentials) => {
if (err) return console.error(err);
const client = new msRest.ServiceClient(credentials);
client.sendRequest({
method: 'GET',
url: 'https://api.example.com/data'
}).then(response => {
console.log(response.body);
}).catch(err => {
console.error(err);
});
});
Custom Middleware
This feature allows you to add custom middleware to the request pipeline. The code sample demonstrates how to log requests and responses by adding a custom policy to the pipeline.
const msRest = require('ms-rest');
const client = new msRest.ServiceClient();
client.pipeline.addPolicy({
sendRequest: (request, next) => {
console.log('Request:', request);
return next(request).then(response => {
console.log('Response:', response);
return response;
});
}
});
client.sendRequest({
method: 'GET',
url: 'https://api.example.com/data'
}).then(response => {
console.log(response.body);
}).catch(err => {
console.error(err);
});
Other packages similar to ms-rest
axios
Axios is a promise-based HTTP client for the browser and Node.js. It provides a simple API for making HTTP requests and handling responses. Compared to ms-rest, Axios is more general-purpose and does not include built-in support for authentication or custom middleware.
request
Request is a simplified HTTP client for Node.js. It provides a straightforward API for making HTTP requests. While it is similar to ms-rest in terms of basic HTTP functionalities, it lacks the advanced features like authentication and custom middleware provided by ms-rest.
node-fetch
Node-fetch is a lightweight module that brings window.fetch to Node.js. It is focused on providing a fetch API in Node.js environments. Compared to ms-rest, node-fetch is more lightweight and does not include advanced features like authentication or custom middleware.
MS-Rest
Infrastructure for serialization/deserialization, error handling, tracing, and http client pipeline configuration. Required by nodeJS client libraries generated using AutoRest.
- Node.js version: 4.x.x or higher
How to Install
npm install ms-rest
Usage
var msrest = require('ms-rest');
Serialization/Deserialization
Features
-
Type checking
- (String, Number, Boolean, ByteArray, Base64Url, Date, DateTime, Enum, TimeSpan, DateTimeRfc1123, UnixTime, Object, Stream, Sequence, Dictionary, Composite, Uuid(as a string))
-
Validation of specified constraints
- ExclusiveMaximum, ExclusiveMinimum, InclusiveMaximum, InclusiveMinimum, MaxItems, MaxLength, MinItems, MinLength, MultipleOf, Pattern, UniqueItems
-
Flattening/Unflattening properties
-
Default Values
-
Model Properties marked as constant are set during serialization, irrespective of they being provided or not
-
Required check (If a model or property is marked required and is not provided in the object then an error is thrown)
-
Readonly check (If a model or property is marked readonly then it is not sent on the wire during, serialization)
-
Serializing Constant values
-
serialize an array of dictionary of primitive values
var mapper = {
type : {
name: 'Sequence',
element: {
type : {
name: 'Dictionary',
value: {
type: {
name: 'Boolean'
}
}
}
}
}
};
var array = [{ 1: true }, { 2: false }, { 1: true, 2: false, 3: true }];
var serializedArray = msRest.serialize(mapper, array, 'arrayObj');
assert.deepEqual(array, serializedArray);
var serializedProduct = msrest.serialize(mapper, productObj, 'productObject');
var deserializedArray = msRest.deserialize(mapper, serializedArray, 'serializedArrayObj');
For more examples on serialization/deserialization with complex types please take a look over here.
Related Projects